package org.tis.tools.abf.module.ac.controller;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.tis.tools.abf.module.ac.controller.request.AcRoleFuncAddRequest;
import org.tis.tools.abf.module.ac.controller.request.AcRoleFuncBatchAddRequest;
import org.tis.tools.abf.module.ac.controller.request.AcRoleFuncDeleteRequest;
import org.tis.tools.abf.module.ac.controller.request.AcRoleFuncQueConditionRequest;
import org.tis.tools.abf.module.ac.entity.AcRoleFunc;
import org.tis.tools.abf.module.ac.service.IAcRoleFuncService;
import org.tis.tools.core.web.controller.BaseController;
import org.tis.tools.core.web.vo.SmartPage;
import org.tis.tools.model.common.ResultVO;
import org.tis.tools.model.log.enums.OperateType;
import org.tis.tools.starter.operatelog.annotation.OperateLog;

import javax.validation.constraints.NotBlank;

/**
 * acRoleFunc的Controller类
 *
 * @author Auto Generate Tools
 * @date 2018/04/23
 */
@RestController
@RequestMapping("/acRoleFuncs")
@Validated
@Slf4j
public class AcRoleFuncController extends BaseController<AcRoleFunc> {

    @Autowired
    private IAcRoleFuncService acRoleFuncService;


    @ApiOperation(value = "增加角色功能",notes = "传入的AcRoleFuncAddRequest作为参数")
    @OperateLog(type = OperateType.ADD, desc = "增加角色功能")
    @PostMapping
    public ResultVO add(@RequestBody @Validated @ApiParam(value = "AcRoleFuncAddRequest",name = "传入的AcRoleFuncAddRequest类", required = true) AcRoleFuncAddRequest acRoleFuncAddRequest) {
        acRoleFuncService.addRoleFunc(acRoleFuncAddRequest);
        return ResultVO.success("增加成功");
    }


    @ApiOperation(value = "修改角色功能",notes = "传入的AcRoleFuncAddRequest作为参数")
    @OperateLog(type = OperateType.UPDATE, desc = "修改角色功能")
    @PutMapping
    public ResultVO update(@RequestBody @Validated @ApiParam(value = "AcRoleFuncAddRequest",name = "传入的AcRoleFuncAddRequest类", required = true) AcRoleFuncAddRequest acRoleFuncAddRequest) {
        AcRoleFunc acRoleFunc = acRoleFuncService.update(acRoleFuncAddRequest);
        return ResultVO.success("修改成功！", acRoleFunc);
    }


    @ApiOperation(value = "根据角色功能GUID删除角色功能",notes = "传入角色功能的GUID(id)作为参数")
    @OperateLog(type = OperateType.DELETE, desc = "根据角色功能GUID删除角色功能")
    @DeleteMapping("/{id}")
    public ResultVO delete(@PathVariable @NotBlank(message = "id不能为空") @ApiParam(value = "id",name = "角色功能的GUID", required = true) String id) {
        AcRoleFunc acRoleFunc = acRoleFuncService.selectById(id);
        if (null == acRoleFunc){
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        acRoleFuncService.deleteRoleFunc(id);
        return ResultVO.success("删除成功", acRoleFunc);
    }


    @ApiOperation(value = "根据GUID查询角色功能",notes = "传入角色功能的GUID(id)作为参数")
    @GetMapping("/{id}")
    public ResultVO detail(@PathVariable @NotBlank(message = "id不能为空") @ApiParam(value = "id",name = "角色功能的GUID", required = true) String id) {
        AcRoleFunc acRoleFunc = acRoleFuncService.selectById(id);
        if (acRoleFunc == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        return ResultVO.success("查询成功", acRoleFunc);
    }


    @ApiOperation(value = "分页查询角色功能",notes = "传入分页信息作为参数")
    @PostMapping("/list")
    public ResultVO list(@RequestBody @Validated @ApiParam(value = "page",name = "分页信息", required = true) SmartPage<AcRoleFunc> page) {
        return ResultVO.success("查询成功", acRoleFuncService.selectPage(page.getPageObj(), page.getConditionWrapper()));
    }


    @ApiOperation(value = "移除某个角色的某个功能",notes = "传入AcRoleFuncDeleteRequest类作为参数")
    @OperateLog(type = OperateType.DELETE, desc = "删除角色的某个功能")
    @DeleteMapping("/removeSomeRoleFunc")
    public ResultVO removeSomeRoleFunc(@RequestBody @Validated @ApiParam(value = "AcRoleFuncDeleteRequest",name = "传入的AcRoleFuncDeleteRequest类", required = true) AcRoleFuncDeleteRequest acRoleFuncDeleteRequest) {
        AcRoleFunc acRoleFunc = new AcRoleFunc();
        acRoleFunc.setGuidRole(acRoleFuncDeleteRequest.getGuidRole());
        acRoleFunc.setGuidApp(acRoleFuncDeleteRequest.getGuidApp());
        acRoleFunc.setGuidApp(acRoleFuncDeleteRequest.getGuidFunc());
        AcRoleFunc acRoleFunc1 = acRoleFuncService.queryRoleFunByCondition(acRoleFunc);
        if (null == acRoleFunc1){
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        acRoleFuncService.deleteRoleFunc(acRoleFunc1.getGuid());
        return ResultVO.success("删除成功", acRoleFunc1);
    }


    @ApiOperation(value = "批量新增和删除角色功能",notes = "传入AcRoleFuncBatchAddRequest类作为参数")
    @OperateLog(type = OperateType.ADD,desc = "批量新增角色功能信息")
    @PostMapping("/batchAdd")
    public ResultVO batchAddClear(@RequestBody @Validated @ApiParam(value = "AcRoleFuncBatchAddRequest",name = "传入的AcRoleFuncBatchAddRequest类", required = true) AcRoleFuncBatchAddRequest batchRequest){
        acRoleFuncService.batchAdd(batchRequest);
        return ResultVO.success("处理成功");
    }

    @ApiOperation(value = "过滤查询角色下功能行为",notes = "传入AcRoleFuncQueConditionRequest类作为参数")
    @PostMapping("/getFilterFuncBehaveByRole")
    public ResultVO getFilterFuncBehaveByRole (@RequestBody @Validated @ApiParam(value = "AcRoleFuncQueConditionRequest",name = "传入的AcRoleFuncQueConditionRequest类", required = true) AcRoleFuncQueConditionRequest conditionRequest){
        return ResultVO.success("查询成功",acRoleFuncService.getFilterFuncBehaveByRole(conditionRequest));
    }

    @ApiOperation(value = "查询所有应用和角色下已有应用",notes = "传入角色GUID作为参数")
    @GetMapping("/queryAppByRole/{roleId}")
    public ResultVO queryAppByRole (@PathVariable @NotBlank(message = "角色id不能为空") @ApiParam(value = "roleId",name = "角色GUID", required = true) String roleId){
        return ResultVO.success("查询成功",acRoleFuncService.queryAppByRole(roleId));
    }


    @ApiOperation(value = "通过角色和应用ID查询该应用下的所有功能和行为,和已分配的功能和行为",notes = "传入AcRoleFuncQueConditionRequest类作为参数")
    @PostMapping("/queryFuncBehaveByRolApp")
    public ResultVO queryFuncBehaveByRolApp (@RequestBody @Validated(AcRoleFuncQueConditionRequest.appId.class) @ApiParam(value = "AcRoleFuncQueConditionRequest",name = "传入的AcRoleFuncQueConditionRequest类", required = true) AcRoleFuncQueConditionRequest conditionRequest){
        return ResultVO.success("查询成功",acRoleFuncService.queryFuncBehaveByRolApp(conditionRequest.getAppId(),conditionRequest.getRoleId()));
    }


    @ApiOperation(value = "通过角色和应用ID查询该应用下的所有功能和行为,和已分配的功能和行为",notes = "传入AcRoleFuncQueConditionRequest类作为参数")
    @PostMapping("/queryFuncBehaveFilterByRolApp")
    public ResultVO queryFuncBehaveFilterByRolApp (@RequestBody @Validated(AcRoleFuncQueConditionRequest.appId.class) @ApiParam(value = "AcRoleFuncQueConditionRequest",name = "传入的AcRoleFuncQueConditionRequest类", required = true) AcRoleFuncQueConditionRequest conditionRequest){
        return ResultVO.success("查询成功",acRoleFuncService.queryFuncBehaveFilterByRolApp(conditionRequest));
    }


    @ApiOperation(value = "查询应用下所有功能和角色下已有功能",notes = "传入AcRoleFuncQueConditionRequest类作为参数")
    @PostMapping("/queryFuncByRole")
    public ResultVO queryFuncByRole (@RequestBody @Validated(AcRoleFuncQueConditionRequest.appId.class) @ApiParam(value = "AcRoleFuncQueConditionRequest",name = "传入的AcRoleFuncQueConditionRequest类", required = true) AcRoleFuncQueConditionRequest conditionRequest){
        return ResultVO.success("查询成功",acRoleFuncService.queryFuncByRole(conditionRequest));
    }


    @ApiOperation(value = "查询功能下所有行为和角色下已有行为",notes = "传入AcRoleFuncQueConditionRequest类作为参数")
    @PostMapping("/queryBehaveByRole")
    public ResultVO queryBehaveByRole (@RequestBody @Validated(AcRoleFuncQueConditionRequest.appId.class) @ApiParam(value = "AcRoleFuncQueConditionRequest",name = "传入的AcRoleFuncQueConditionRequest类", required = true) AcRoleFuncQueConditionRequest conditionRequest){
        return ResultVO.success("查询成功",acRoleFuncService.queryBehaveByRole(conditionRequest));
    }
}

